<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreateMemberCardOrderTable extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $table = $this->table('mcard_member_card_order', ['id' => false, 'primary_key' => ['id'], 'comment' => '会员卡订单表']);
        
        $table->addColumn('id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'identity' => true, 'comment' => '主键ID'])
              ->addColumn('order_no', 'string', ['limit' => 32, 'null' => false, 'comment' => '订单编号'])
              ->addColumn('user_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'comment' => '用户ID'])
              ->addColumn('member_card_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'comment' => '会员卡ID'])
              ->addColumn('card_name', 'string', ['limit' => 100, 'null' => false, 'comment' => '会员卡名称'])
              ->addColumn('price', 'decimal', ['precision' => 10, 'scale' => 2, 'null' => false, 'comment' => '购买价格'])
              ->addColumn('validity_period', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'comment' => '有效期(天)'])
              ->addColumn('start_time', 'timestamp', ['null' => true, 'comment' => '生效开始时间'])
              ->addColumn('end_time', 'timestamp', ['null' => true, 'comment' => '生效结束时间'])
              ->addColumn('status', 'integer', ['limit' => 1, 'null' => false, 'default' => 0, 'comment' => '订单状态：0=待支付，1=已支付，2=已取消'])
              ->addColumn('pay_time', 'timestamp', ['null' => true, 'comment' => '支付时间'])
              ->addColumn('pay_type', 'string', ['limit' => 20, 'null' => true, 'comment' => '支付方式'])
              ->addColumn('payment_id', 'string', ['limit' => 64, 'null' => true, 'comment' => '支付ID'])
              ->addColumn('create_time', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
              ->addColumn('update_time', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'comment' => '更新时间'])
              ->addColumn('delete_time', 'timestamp', ['null' => true, 'comment' => '删除时间']);

        // 添加索引
        $table->addIndex(['order_no'], ['unique' => true])
              ->addIndex(['user_id'])
              ->addIndex(['member_card_id'])
              ->addIndex(['status'])
              ->addIndex(['create_time']);

        $table->create();
    }
}